﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.Payments.Data;

namespace blackhouse.Payments {
    public class PaymentType {

        #region Fields

        private IPaymentsDataAccess dataAccess;

        #endregion

        #region Properties

        public string PaymentKey { get; private set; }
        public double MinimumChargeValue { get; private set; }
        public double MaximumChargeValue { get; private set; }
        public int AutoCancelDays { get; private set; }
        public string Description { get; private set; }
        public bool Exist { get; private set; }

        #endregion

        #region Constructors

        public PaymentType(string paymentKey) {
            this.dataAccess = PaymentsDataFactory.Instance().GetPaymentDataAccess();
            PaymentTypeData data = this.dataAccess.GetPaymentType(paymentKey);
            if (data.PaymentKey == paymentKey)
                this.SetInstance(data);
        }

        #endregion

        #region Methods

        private void SetInstance(PaymentTypeData data) {
            this.PaymentKey = data.PaymentKey;
            this.MinimumChargeValue = data.MinimumChargeValue;
            this.MaximumChargeValue = data.MaximumChargeValue;
            this.AutoCancelDays = data.AutoCancelDays;
            this.Description = data.Description;
            this.Exist = true;
        }

        #endregion

        #region Predefined Payment Types

        private static PaymentType noDefined;
        private static PaymentType multiBank;
        private static PaymentType bzWbk;
        private static PaymentType pekao24;
        private static PaymentType inteligo;
        private static PaymentType nordea;
        private static PaymentType iPko;
        private static PaymentType bph;
        private static PaymentType ing;
        private static PaymentType creditAgricole;
        private static PaymentType eurobank;
        private static PaymentType meritum;
        private static PaymentType alior;
        private static PaymentType polbank;
        private static PaymentType millenium;
        private static PaymentType kredytBank;
        private static PaymentType bgz;
        private static PaymentType deutscheBank;
        private static PaymentType raiffeisenBank;
        private static PaymentType citibank;
        private static PaymentType investBank;
        private static PaymentType getinBank;
        private static PaymentType bankPocztowy;
        private static PaymentType creditCard;
        private static PaymentType bankTransfer;
        private static PaymentType test;

        public static PaymentType NoDefined {
            get {
                if (noDefined == null)
                    noDefined = new PaymentType("no_def");
                return noDefined;
            }
        }

        public static PaymentType MultiBank {
            get {
                if (multiBank == null)
                    multiBank = new PaymentType("n");
                return multiBank;
            }
        }

        public static PaymentType BzWbk {
            get {
                if (bzWbk == null)
                    bzWbk = new PaymentType("w");
                return bzWbk;
            }
        }

        public static PaymentType Pekao24 {
            get {
                if (pekao24 == null)
                    pekao24 = new PaymentType("o");
                return pekao24;
            }
        }

        public static PaymentType Inteligo {
            get {
                if (inteligo == null)
                    inteligo = new PaymentType("i");
                return inteligo;
            }
        }

        public static PaymentType Nordea {
            get {
                if (nordea == null)
                    nordea = new PaymentType("d");
                return nordea;
            }
        }

        public static PaymentType IPko {
            get {
                if (iPko == null)
                    iPko = new PaymentType("p");
                return iPko;
            }
        }

        public static PaymentType Bph {
            get {
                if (bph == null)
                    bph = new PaymentType("h");
                return bph;
            }
        }

        public static PaymentType Ing {
            get {
                if (ing == null)
                    ing = new PaymentType("g");
                return ing;
            }
        }

        public static PaymentType CreditAgricole {
            get {
                if (creditAgricole == null)
                    creditAgricole = new PaymentType("l");
                return creditAgricole;
            }
        }

        public static PaymentType Eurobank {
            get {
                if (eurobank == null)
                    eurobank = new PaymentType("u");
                return eurobank;
            }
        }

        public static PaymentType Meritum {
            get {
                if (meritum == null)
                    meritum = new PaymentType("me");
                return meritum;
            }
        }

        public static PaymentType Alior {
            get {
                if (alior == null)
                    alior = new PaymentType("ab");
                return alior;
            }
        }

        public static PaymentType Polbank {
            get {
                if (polbank == null)
                    polbank = new PaymentType("wp");
                return polbank;
            }
        }

        public static PaymentType Millenium {
            get {
                if (millenium == null)
                    millenium = new PaymentType("wm");
                return millenium;
            }
        }

        public static PaymentType KredytBank {
            get {
                if (kredytBank == null)
                    kredytBank = new PaymentType("wk");
                return kredytBank;
            }
        }

        public static PaymentType Bgz {
            get {
                if (bgz == null)
                    bgz = new PaymentType("wg");
                return bgz;
            }
        }

        public static PaymentType DeutscheBank {
            get {
                if (deutscheBank == null)
                    deutscheBank = new PaymentType("wd");
                return deutscheBank;
            }
        }

        public static PaymentType RaiffeisenBank {
            get {
                if (raiffeisenBank == null)
                    raiffeisenBank = new PaymentType("wr");
                return raiffeisenBank;
            }
        }

        public static PaymentType Citibank {
            get {
                if (citibank == null)
                    citibank = new PaymentType("wc");
                return citibank;
            }
        }

        public static PaymentType InvestBank {
            get {
                if (investBank == null)
                    investBank = new PaymentType("wn");
                return investBank;
            }
        }

        public static PaymentType GetinBank {
            get {
                if (getinBank == null)
                    getinBank = new PaymentType("wi");
                return getinBank;
            }
        }

        public static PaymentType BankPocztowy {
            get {
                if (bankPocztowy == null)
                    bankPocztowy = new PaymentType("wy");
                return bankPocztowy;
            }
        }

        public static PaymentType CreditCard {
            get {
                if (creditCard == null)
                    creditCard = new PaymentType("c");
                return creditCard;
            }
        }

        public static PaymentType BankTransfer {
            get {
                if (bankTransfer == null)
                    bankTransfer = new PaymentType("b");
                return bankTransfer;
            }
        }

        public static PaymentType Test {
            get {
                if (test == null)
                    test = new PaymentType("t");
                return test;
            }
        }


        #endregion


        #region Equality

        public override bool Equals(Object obj) {
            PaymentType p = obj as PaymentType;
            if ((object)p == null) {
                return false;
            }

            return this.PaymentKey == p.PaymentKey;
        }

        public bool Equals(PaymentType p) {
            return this.PaymentKey == p.PaymentKey;
        }

        public override int GetHashCode() {
            int[] c = this.PaymentKey.ToCharArray().Select(ch => (int)ch).ToArray();
            for (int xc = 0; xc < c.Length; xc++) {
                c[xc] = c[xc] * ((int)Math.Pow(10, xc));
            }
            return c.Sum();
        }

        public static bool operator ==(PaymentType a, PaymentType b) {
            if (System.Object.ReferenceEquals(a, b))
                return true;

            if (((object)a == null) || ((object)b == null))
                return false;

            return a.PaymentKey == b.PaymentKey;
        }

        public static bool operator !=(PaymentType a, PaymentType b) {
            return !(a == b);
        }

        #endregion

    }
}